package com.pig.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.pig.dao.OrderItemMapper;
import com.pig.dao.OrderMapper;
import com.pig.pojo.addr.Addr;
import com.pig.pojo.order.Order;
import com.pig.pojo.order.OrderItem;
import com.pig.service.order.DubboOrderService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
public class DubboOrderServiceImpl implements DubboOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public List<Addr> findUserAddr(Integer userId) {
        List<Addr> addrList=orderMapper.findAddrByUserId(userId);
        return addrList;
    }

    @Override
    public void savaOrder(Order order) {
        order.setStatus(0);
        order.setDeliveryCompany("佩奇配送");
        order.setCreateTime(new Date());
        order.setPromotionAmount(0);
        if (order.getTotalAmount()<49){
            order.setFreightAmount(5);
            order.setPayAmount(order.getTotalAmount()+5);
        }else {
            order.setFreightAmount(0);
            order.setPayAmount(order.getTotalAmount());
        }
        System.out.println(order.toString());
        orderMapper.insert(order);
    }

    @Override
    public void insertOrderItemByOrderId(List<OrderItem> userItems, String orderId) {
        //orderItemMapper.savaOrderItemByOrderId(orderItem);
        System.out.println(userItems);
        System.out.println(orderId);
        for (OrderItem orderItem : userItems) {
            orderItem.setOrderId(orderId);
            orderItem.setOrderItemId(System.currentTimeMillis());
            System.out.println(orderItem.getOrderItemId());
            System.out.println(orderItem.getUserId());
            orderItemMapper.insert(orderItem);
        }

    }

    @Override
    public void insertAddr(Long userId) {
        Addr addr=new Addr();
        addr.setAddrId(userId);
        addr.setUserId(userId);
        addr.setReceiverName("王思聪");
        addr.setReceiverPhone("13855668888");
        addr.setReceiverProvince("广东省");
        addr.setReceiverCity("珠海市");
        addr.setReceiverRegion("达内集团");
        addr.setReceiverAddress("（此为系统默认数据）");
        addr.setCreatedTime(new Date());
        addr.setModifiedTime(new Date());
        System.out.println(addr);
       orderMapper.insertAddr(addr);
    }
}
